package com.bsoft.exchange.service;



import java.sql.SQLException;
import java.util.List;

import com.bsoft.exchange.dao.portal.HospitalInformationMapper;
import com.bsoft.exchange.domain.HospitalInformation;
import com.bsoft.exchange.domain.QueueinformationDomain;
import com.bsoft.exchange.domain.WaitingInformation;
import com.bsoft.exchange.util.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.bsoft.exchange.dao.his.QueuesMapper;
import com.bsoft.exchange.domain.ListQueuesDomain;
import com.bsoft.exchange.pojo.ListQueuesPojo;

@Service
public class QueuesService {
    @Autowired
    QueuesMapper queuesMapper;

    @Autowired
    PublicService publicService;
    private List<String> brdaList;
    @Autowired
    CacheSupport cacheSupport;
    @Autowired
    HospitalInformationMapper hospitalInformationMapper;
    /**
     * 获取排队就诊信息
     * @param listQueuesPojo
     * @return
     * @throws SQLException
     */
    public BaseResponse<List<ListQueuesDomain>> listQueues(ListQueuesPojo listQueuesPojo) throws SQLException {
        BaseResponse<List<ListQueuesDomain>>baseResponse=new BaseResponse<>();
        if (!listQueuesPojo.getQueryType().equals(1)){
        	if(listQueuesPojo.getQueryType().equals(2)){
        		 brdaList = publicService.getBRIDist(listQueuesPojo.getOrganizationId(),listQueuesPojo.getQueryType(), listQueuesPojo.getPatientIdentityCardList(), null);
        	}else{
            brdaList = publicService.getBRIDist(listQueuesPojo.getOrganizationId(),listQueuesPojo.getQueryType(), null, listQueuesPojo.getPatientMedicalCardList());
        	}
        }else{
            brdaList = listQueuesPojo.getPatientIdList();
        }
        if (brdaList==null){
            baseResponse.setCode(Constant.errorTag);
            baseResponse.setMessage("数据库查询失败,请检查参数设置");
            return baseResponse;
        }
        if (brdaList.size()==0){
            baseResponse.setCode(Constant.successResponse);
            baseResponse.setMessage("获取不到该病人信息");
            return baseResponse;
        }


        List<ListQueuesDomain> queuesList = queuesMapper.listQueues(brdaList,listQueuesPojo.getOrganizationId());
        List<HospitalInformation> hospitalInformationList=cacheSupport.getJgmcList(listQueuesPojo.getOrganizationId(), listQueuesPojo.getOrganizationId()+"jgmc",hospitalInformationMapper);
        for (ListQueuesDomain listQueuesDomain:queuesList){
            if (listQueuesDomain.getPJSJ1()!=null&&!"".equals(listQueuesDomain.getPJSJ1())){
                listQueuesDomain.setAvgTime(listQueuesDomain.getPJSJ1());
            }else if (listQueuesDomain.getPJSJ2()!=null&&!"".equals(listQueuesDomain.getPJSJ2())){
                listQueuesDomain.setAvgTime(listQueuesDomain.getPJSJ2());
            }else{
                listQueuesDomain.setAvgTime("5");
            }
           String pdrs=String.valueOf(listQueuesDomain.getFrontNumber()) ;
           String jzsj= SumPriceUtil.
                   getRidePrice(pdrs,listQueuesDomain.getAvgTime());//获取还要用多久（分钟）
           //计算预计就诊时间：现在时间加上需要用多久的时间
            long myTime=System.currentTimeMillis()+Long.parseLong(jzsj)*60*1000;
            listQueuesDomain.setExpectTime(TimeUtil.longToStr(myTime));
            //确定返回的诊疗卡参数
            if (listQueuesDomain.getJZKH()!=null&&!"".equals(listQueuesDomain.getJZKH())){
                listQueuesDomain.setPatientMedicalCardType(2);
                listQueuesDomain.setPatientMedicalCardNumber(listQueuesDomain.getJZKH());
            }else  if (listQueuesDomain.getYBKH()!=null&&!"".equals(listQueuesDomain.getYBKH())){
                listQueuesDomain.setPatientMedicalCardType(3);
                listQueuesDomain.setPatientMedicalCardNumber(listQueuesDomain.getYBKH());
            }else if (listQueuesDomain.getMZHM()!=null&&!"".equals(listQueuesDomain.getMZHM())){
                listQueuesDomain.setPatientMedicalCardType(5);
                listQueuesDomain.setPatientMedicalCardNumber(listQueuesDomain.getMZHM());
            }else{
                listQueuesDomain.setPatientMedicalCardType(1);
                listQueuesDomain.setPatientMedicalCardNumber(null);
            }
            //获取当前时间
            listQueuesDomain.setQueuesUpdateTime(TimeUtil.getNowDate());
            for (HospitalInformation jgxx:hospitalInformationList){
                if (listQueuesDomain.getOrganizationId().equals(jgxx.getOrganizationId())){
                    listQueuesDomain.setOrganizationName(jgxx.getOrganizationName());
                }
            }
        }
        if(queuesList == null || queuesList.size() < 1){
        	baseResponse.setCode(Constant.successResponse);
            baseResponse.setMessage("未查询到数据!");
            
            return baseResponse;
        }
        baseResponse.setCode(Constant.successTag);
        baseResponse.setData(queuesList);
        
        return baseResponse;
    }
    /**
     * 排队信息查询
     */
    QueueinformationDomain maxData;
    public BaseResponse<QueueinformationDomain>getQueueinInfo(String jgid,String queuesId){
        BaseResponse<QueueinformationDomain>listBaseResponse=new BaseResponse<>();
        QueueinformationDomain dataList=queuesMapper.queueinformation(jgid,queuesId);
        if (dataList==null){
            listBaseResponse.setCode(Constant.successResponse);
            listBaseResponse.setMessage("未查询到数据!");
            return listBaseResponse;
        }
            for (WaitingInformation waitingInformation:dataList.getWaitingInformationList()) {

                if (!TextUtils.isEmpty(waitingInformation.getJZKH())){
                    waitingInformation.setPatientMedicalCardType(2);
                    waitingInformation.setPatientMedicalCardNumber(waitingInformation.getJZKH());
                }else if (!TextUtils.isEmpty(waitingInformation.getYBKH())){
                    waitingInformation.setPatientMedicalCardType(3);
                    waitingInformation.setPatientMedicalCardNumber(waitingInformation.getYBKH());
                }else{
                   waitingInformation.setPatientMedicalCardType(5);
                   waitingInformation.setPatientMedicalCardNumber(waitingInformation.getMZHM());
                }
            waitingInformation.setCardType("01");
            waitingInformation.setCardNumber(waitingInformation.getSFZH());
            String pdrs = String.valueOf(waitingInformation.getFrontNumber());
            String jzsj = SumPriceUtil.getRidePrice(String.valueOf(dataList.getAvgTime()), pdrs);//获取还要用多久（分钟）
            //计算预计就诊时间：现在时间加上需要用多久的时间
            long myTime = System.currentTimeMillis() + Long.parseLong(jzsj) * 60 * 1000;
            waitingInformation.setExpectTime(TimeUtil.longToStr(myTime));
            }

        listBaseResponse.setCode(Constant.successTag);
        listBaseResponse.setData(dataList);
        listBaseResponse.setMessage("查询成功");
        return listBaseResponse;
    }
}
